package com.apes.fn.fico.expense.platformFee.process;

import com.apes.fn.business.insurance.modle.InsuranceOrder;
import com.apes.fn.business.insurance.repository.InsuranceOrderRepository;
import com.apes.fn.fico.expense.platformFee.CommonFeeProcess;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 车险订单生成平台费用细项单据
 */
@Service
public class InsuranceOrderFeeProcess extends CommonFeeProcess {

    @Autowired
    private InsuranceOrderRepository insuranceOrderRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        InsuranceOrder insuranceOrder = insuranceOrderRepository.findOne(id);
        // 数据检查
        if (LogicalUtil.notIn(insuranceOrder.getState(), "6", "7", "8")) return;
        createExpense(MapUtil.mapper("order", insuranceOrder));     // 为了传值方便，就用 Map 进行封装
    }

    /**
     * 车险销售管理费
     *
     * @param map {"order": InsuranceOrder}
     */
    @Override
    public Map<String, Object> getExpenseModel(Map map) {
        InsuranceOrder insuranceOrder = (InsuranceOrder) map.get("order");
        double paymentAmount;
        double expenseAmount;
        if ("6".equals(insuranceOrder.getState())) {
            expenseAmount = insuranceOrder.getInsuranceManagementFee();
            paymentAmount = insuranceOrder.getCommercialInsurance() + insuranceOrder.getCompulsoryInsurance();
        } else if ("7".equals(insuranceOrder.getState())) {
            expenseAmount = insuranceOrder.getInsuranceManagementFee() * -1;
            paymentAmount = insuranceOrder.getCommercialInsurance() + insuranceOrder.getCompulsoryInsurance();
        } else if ("8".equals(insuranceOrder.getState())){
            expenseAmount = insuranceOrder.getInsuranceManagementFee() * -1;
            paymentAmount = insuranceOrder.getCommercialAbolishAmount() + insuranceOrder.getCompulsoryAbolishAmount();
        } else return null;
        return MapUtil.mapper("order", insuranceOrder.getId(), "orderType", "InsuranceOrder",
                "source", insuranceOrder.getId(), "sourceBusinessType", "InsuranceOrder",
                "summary", "", "incomeTypeId", "D01", "expenseDetailsTypeId", "002050",
                "paymentAmount", paymentAmount, "expenseAmount", expenseAmount,"date",insuranceOrder.getCreateDate(),
                "deptId", insuranceOrder.getDept().getId(), "companyId", insuranceOrder.getCompany().getId());
    }

    @Override
    public boolean support(String type) {
        return "InsuranceOrder".equals(type);
    }
}
